package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/loginOut")//退
// 出登录
public class LoginOutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //使session失效
        request.getSession().invalidate();
        Cookie [] cookies = request.getCookies();
        Cookie cookie01 = null;
        Cookie cookie02 = null;
        for (Cookie cookie : cookies){
            if (cookie.getName().equals("name")){
                cookie01 =cookie;
            }
            if (cookie.getName().equals("password")){
                cookie02 =cookie ;
            }
        }
        if (cookie01 != null){
            cookie01.setMaxAge(0);
            cookie01.setPath(request.getContextPath() + "/");
            response.addCookie(cookie01);
        }
        if (cookie02 != null){
            cookie02.setMaxAge(0);
            cookie02.setPath(request.getContextPath() + "/");
            response.addCookie(cookie02);
        }
        //重定向回登录页面
        response.sendRedirect("login.jsp");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

